'''
# -*- coding:utf-8 -*-
# @Project: DevOps_api
# @Date: 2023/3/26 - 22:25
# @Author: ysg
# @Describe: 

fallback.py
'''
import os
import shutil
import sys

ver_name = sys.argv[1]

# ver_name = '000212'
back_path = '/ysg/back_up/'
back_dir_path = os.path.join(back_path, ver_name)
ver_file_path = os.path.join(back_path, ver_name + '.ver')


def copy_dir():
    '''
    读取 ver_name 对应的目录与版本记录文件
    :return:
    '''
    # 版本记录文件中 路径如果为 dir 且状态为 False，则直接删除 dir 目录
    with open(ver_file_path, 'r', encoding='utf-8') as fr:
        source_path = fr.readline().replace('\n', '')
        while 1:
            info = fr.readline()
            if not info: break
            path, status = info.replace('\n', '').split('||')
            if os.path.isdir(path) and status == 'False':
                shutil.rmtree(path)
            elif os.path.isfile(path) and status == 'False':
                os.remove(path)
            elif os.path.isfile(path) and status == 'True':
                back_file_path = path.replace(source_path, back_dir_path + '/', 1)
                shutil.copy(back_file_path, path)
    print(f'{ver_name}：回退完成')


copy_dir()

